package com.rat.datadictionary.config;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;



/**
* @Description: 数据库配置类
* @Author: Howe
* @Date: 2021/1/14 17:39
**/
@Configuration
public class DataSourceConfig {


    /**
    * @Description:  加载默认数据库
    * @Param:
    * @Return:
    * @Author: Howe
    * @Date: 2021/1/14 17:40
    **/
    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource defaultDataSource() {
        return DataSourceBuilder.create().build();
    }


    /**
    * @Description:  配置动态数据源
    * @Param:
    * @Return:
    * @Author: Howe
    * @Date: 2021/1/14 17:40
    **/
    @Bean
    @Primary
    @DependsOn({"springUtils", "defaultDataSource"})
    public DynamicDataSource dataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(DynamicDataSource.dataSourcesMap);
        return dynamicDataSource;
    }
}
